package cn.rxls.community.service.impl;

import cn.rxls.community.dto.maintenance.MaintenanceAddDTO;
import cn.rxls.community.dto.maintenance.MaintenancePageDTO;
import cn.rxls.community.dto.maintenance.MaintenanceUpdateDTO;
import cn.rxls.community.entity.Maintenance;
import cn.rxls.community.entity.Orders;
import cn.rxls.community.enums.MaintenanceStateEnum;
import cn.rxls.community.enums.OrdersStateEnum;
import cn.rxls.community.enums.OrdersTypeEnum;
import cn.rxls.community.enums.ResultEnum;
import cn.rxls.community.exception.BusinessException;
import cn.rxls.community.mapper.MaintenanceMapper;
import cn.rxls.community.mapper.OrdersMapper;
import cn.rxls.community.result.Result;
import cn.rxls.community.service.MaintenanceService;
import cn.rxls.community.util.HttpUtil;
import cn.rxls.community.vo.maintenance.MaintenanceCategoryVO;
import cn.rxls.community.vo.maintenance.MaintenanceVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * 维修(Maintenance)表服务实现类
 */
@Service("maintenanceService")
@Transactional
public class MaintenanceServiceImpl extends ServiceImpl<MaintenanceMapper, Maintenance> implements MaintenanceService {

    private final MaintenanceMapper maintenanceMapper;
    private final OrdersMapper ordersMapper;

    MaintenanceServiceImpl(MaintenanceMapper maintenanceMapper,OrdersMapper ordersMapper) {
        this.maintenanceMapper = maintenanceMapper;
        this.ordersMapper = ordersMapper;
    }

    /**
     * 分页查询维修内容
     *
     * @param dto 分页对象
     * @return 多条维修信息
     */
    @Override
    public List<MaintenanceVO> selectPageByState(MaintenancePageDTO dto) {
        String uid = String.valueOf(HttpUtil.getUserLoginInfo().get("uid"));
        dto.setPage((dto.getPage() - 1) * dto.getSize());
        Integer state = MaintenanceStateEnum.getCodeByMsg(dto.getState());
        return maintenanceMapper.selectPageByState(dto.getPage(), dto.getSize(), Long.parseLong(uid),state);
    }

    /**
     * 根据维修id查询维修内容
     *
     * @param id 维修id
     * @return 单条维修内容数据
     */
    @Override
    public MaintenanceVO selectDetail(Long id) {
        LambdaQueryWrapper<Maintenance> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Maintenance::getMaintenanceId, id)
                .select(Maintenance::getMaintenanceId,
                        Maintenance::getMaintenanceAddress,
                        Maintenance::getMaintenanceImg,
                        Maintenance::getMaintenanceTitle,
                        Maintenance::getMaintenanceMsg,
                        Maintenance::getMaintenanceState,
                        Maintenance::getMaintenanceTime,
                        Maintenance::getMaintenanceResultsImg,
                        Maintenance::getUpdateTime,
                        Maintenance::getMaintenancePhone,
                        Maintenance::getRemark
                );
        Maintenance info = maintenanceMapper.selectOne(wrapper);
        MaintenanceVO maintenanceVO = new MaintenanceVO();
        Optional.ofNullable(info).ifPresent(s -> {
            BeanUtils.copyProperties(info, maintenanceVO);
        });
        return maintenanceVO;
    }

    /**
     * 维修新增
     *
     * @param dto 参数对象
     * @return 是否成功
     */
    @Override
    public Boolean insertMaintenance(MaintenanceAddDTO dto) {
        //获取用户信息
        String uid = String.valueOf(HttpUtil.getUserLoginInfo().get("uid"));

        //新增维修内容信息
        Maintenance maintenance = new Maintenance();
        maintenance.setMaintenanceAddress(dto.getMaintenanceAddress());
        maintenance.setMaintenanceImg(dto.getMaintenanceImg());
        maintenance.setMaintenanceMsg(dto.getMaintenanceMsg());
        maintenance.setMaintenanceTitle(dto.getMaintenanceTitle());
        maintenance.setMaintenancePhone(dto.getMaintenancePhone());
        maintenance.setRemark(dto.getRemark());
        maintenance.setMaintenanceTime(dto.getMaintenanceTime());
        maintenance.setMaintenanceUser(Long.valueOf(uid));
        maintenance.setMaintenanceState(MaintenanceStateEnum.SUBMITTED.code);
        if(maintenanceMapper.insert(maintenance) < 0){
            throw new BusinessException(ResultEnum.ERROR_ORDERS_MAINTENANCE);
        }

        //创建维修订单
        Orders orders = new Orders();
        // 维修订单编号
        orders.setOrdersCategory(OrdersTypeEnum.ORDERS_MAINTENANCE.msg);
        orders.setOrdersMsg(maintenance.getMaintenanceTitle());
        orders.setProductsId(maintenance.getMaintenanceId());
        orders.setUserId(Long.valueOf(uid));
        orders.setOrdersState(OrdersStateEnum.SUBMITTED.code);

        return ordersMapper.insert(orders) > 0 ;
    }

    /**
     * 维修内容变更
     *
     * @param dto 参数对象
     * @return 是否成功
     */
    @Override
    public Boolean updateMaintenance(MaintenanceUpdateDTO dto) {
        LambdaQueryWrapper<Maintenance> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Maintenance::getMaintenanceId, dto.getMaintenanceId());
        Maintenance maintenance = new Maintenance();
        BeanUtils.copyProperties(dto, maintenance);
        return maintenanceMapper.update(maintenance, wrapper) > 0;
    }

    /**
     * 根据类型id查询维修分类
     *
     * @param id 类型
     * @return 多条维修分类数据
     */
    @Override
    public List<MaintenanceCategoryVO> selectMaintenanceCategoryById(Long id) {
        return maintenanceMapper.selectMaintenanceCategoryById(id);
    }
}

